package com.zmx.patientend.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.R;
import com.zmx.patientend.pojo.ChatPatients;
import com.zmx.patientend.pojo.ChatPojo;
import com.zmx.patientend.view.activity.PrescriptionMouldDetailsActivity;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/9/4  9:47
 * 功 能：
 */

public class ChatMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ChatPojo> lists;

    //三个final分别代表三个不同的布局
    public static final int ITEMONE = 1;
    public static final int ITEMTWO = 2;
    public static final int ITEMTHREE = 3;

    //上下文
    private Context context;
    private Activity m_activity;


    private OrderImageAdapter adapter;
    private List<String> image_url;

    /**
     * 构造方法
     *
     * @param context
     */
    public ChatMessageAdapter(Context context, List<ChatPojo> lists,Activity m_activity) {
        this.context = context;
        this.m_activity =m_activity;
        this.lists = lists;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        //这时候就要根据这个i来判断加哪一个布局了
        View inflate = null;
        RecyclerView.ViewHolder viewHolder = null;

        //根据i返回不同布局
        switch (i) {

            case ITEMONE:
                inflate = LayoutInflater.from(context).inflate(R.layout.chat_user, viewGroup, false);
                viewHolder = new OneItemHolder(inflate);
                break;

            case ITEMTWO:
                inflate = LayoutInflater.from(context).inflate(R.layout.chat_login, viewGroup, false);
                viewHolder = new TwoItemHolder(inflate);
                break;
            case ITEMTHREE:
                inflate = LayoutInflater.from(context).inflate(R.layout.adapter_chat_p_details, viewGroup, false);
                viewHolder = new ThreeItemHolder(inflate);
                break;

        }

        //返回布局
        return viewHolder;
    }

    /**
     * 绑定控件，这里可以写一些事件方法等
     *
     * @param viewHolder
     * @param i
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        ChatPojo chat = lists.get(i);
        //如果当前的 viewHolder 属于 OneItemHolder 则执行
        if (viewHolder instanceof OneItemHolder) {

            // 设置数据
            if (chat.getType() == 0){

                ((OneItemHolder) viewHolder).mMsg.setVisibility(View.VISIBLE);
                ((OneItemHolder) viewHolder).msg_image.setVisibility(View.GONE);
                ((OneItemHolder) viewHolder).mMsg.setText(chat.getContent());

            }else if (chat.getType() == 1){

                ((OneItemHolder) viewHolder).mMsg.setVisibility(View.GONE);
                ((OneItemHolder) viewHolder).msg_image.setVisibility(View.VISIBLE);
                Glide.with(context).load(chat.getContent()).into(((OneItemHolder) viewHolder).msg_image);
                ((OneItemHolder) viewHolder).msg_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        imgMax(chat.getContent());
                    }
                });

            }else{

                ((OneItemHolder) viewHolder).mMsg.setVisibility(View.VISIBLE);
                ((OneItemHolder) viewHolder).msg_image.setVisibility(View.GONE);
                ((OneItemHolder) viewHolder).mMsg.setTextColor(context.getResources().getColor(R.color.there));
                ((OneItemHolder) viewHolder).mMsg.setText("点击查看处方");
                ((OneItemHolder) viewHolder).mMsg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putString("temp_id",lists.get(i).getContent()+"");
                        Intent intent = new Intent(context,PrescriptionMouldDetailsActivity.class);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    }
                });

            }

            Glide.with(context).load(chat.getAvatar()).into(((OneItemHolder) viewHolder).head);
            ((OneItemHolder) viewHolder).name.setText(chat.getName());
            ((OneItemHolder) viewHolder).time.setText("回复时间：" + chat.getReplytime());
            //处理刷新数据后闪屏问题
//            ((OneItemHolder) viewHolder).head.setScaleType(ImageView.ScaleType.CENTER_CROP);

            //写绑定或这写事件可以如下
//            ((OneItemHolder) viewHolder).one_text.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(context, "Toast就想找个女朋友", Toast.LENGTH_SHORT).show();
//                }
//            });

        } else if (viewHolder instanceof TwoItemHolder) {

            // 设置数据
            if (chat.getType() == 0){

                ((TwoItemHolder) viewHolder).mMsg.setVisibility(View.VISIBLE);
                ((TwoItemHolder) viewHolder).msg_image.setVisibility(View.GONE);
                ((TwoItemHolder) viewHolder).mMsg.setText(chat.getContent());

            }else if (chat.getType() == 1){

                ((TwoItemHolder) viewHolder).mMsg.setVisibility(View.GONE);
                ((TwoItemHolder) viewHolder).msg_image.setVisibility(View.VISIBLE);
                Glide.with(context).load(chat.getContent()).into(((TwoItemHolder) viewHolder).msg_image);
                ((TwoItemHolder) viewHolder).msg_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        imgMax(chat.getContent());
                    }
                });

            }else{

                ((TwoItemHolder) viewHolder).mMsg.setVisibility(View.VISIBLE);
                ((TwoItemHolder) viewHolder).msg_image.setVisibility(View.GONE);
                ((TwoItemHolder) viewHolder).mMsg.setTextColor(context.getResources().getColor(R.color.there));
                ((TwoItemHolder) viewHolder).mMsg.setText("点击查看处方");
                ((TwoItemHolder) viewHolder).mMsg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {


                        Bundle bundle = new Bundle();
                        bundle.putString("temp_id",lists.get(i).getContent()+"");
                        Intent intent = new Intent(context,PrescriptionMouldDetailsActivity.class);
                        intent.putExtras(bundle);
                        context.startActivity(intent);


                    }
                });

            }

            Glide.with(context).load(chat.getAvatar()).into(((TwoItemHolder) viewHolder).head);
            ((TwoItemHolder) viewHolder).name.setText(chat.getName());
            ((TwoItemHolder) viewHolder).time.setText("回复时间：" + chat.getReplytime());

        } else if (viewHolder instanceof ThreeItemHolder) {

            ChatPatients cps = lists.get(i).getChatPatients();

            image_url = cps.getImageUrl();
            ((ThreeItemHolder) viewHolder).content.setText(lists.get(i).getChatPatients().getContext());
            ((ThreeItemHolder) viewHolder).pMessage.setText(cps.getName());
            ((ThreeItemHolder) viewHolder).time.setText(cps.getTime());

            ((ThreeItemHolder) viewHolder).doctor_name.setText(cps.getDoctor_name());

            if (cps.getDoctor_title() == null || cps.getDoctor_title().equals("null") ) {

                ((ThreeItemHolder) viewHolder).doctor_title.setVisibility(View.GONE);
                ((ThreeItemHolder) viewHolder).hosp_name.setVisibility(View.GONE);
                Glide.with(context).load(R.mipmap.zjtd_head).into(((ThreeItemHolder) viewHolder).doctor_head);

            }else{

                ((ThreeItemHolder) viewHolder).doctor_title.setText(cps.getDoctor_title());
                ((ThreeItemHolder) viewHolder).hosp_name.setText(cps.getHosp_name());
                Glide.with(context).load(cps.getHead_image()).into(((ThreeItemHolder) viewHolder).doctor_head);
            }


            ((ThreeItemHolder) viewHolder).doctor_context.setText("收费：98.00");

            ((ThreeItemHolder) viewHolder).image_recycler.setLayoutManager(new GridLayoutManager(context, 3));
            adapter = new OrderImageAdapter(m_activity);
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                protected void onItemClick(View view, int listPosition) {

                    imgMax(image_url.get(listPosition));

                }
            });
            ((ThreeItemHolder) viewHolder).image_recycler.setAdapter(adapter);
            adapter.setListAndNotifyDataSetChanged(image_url);

        }
    }

    /**
     * 返回条目总数量，假设16个条目
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return lists.size();
    }

    /**
     * 返回条目类型(这里就做个简单的判断区分)
     *
     * @param position 代表第几个条目
     * @return
     */
    @Override
    public int getItemViewType(int position) {

        Log.e("类型","类型"+lists.get(position).getRole());

        if (lists.get(position).getRole() == 1) {
            return ITEMONE;
        } else if (lists.get(position).getRole() == 2) {
            return ITEMTWO;
        } else {
            return ITEMTHREE;
        }
    }

    /**
     * 第一个布局的Holder，要继承自RecyclerView.ViewHolder，这里你可以绑定控件
     */
    class OneItemHolder extends RecyclerView.ViewHolder {

        //举例
        TextView mMsg, name, time;
        CircleImageView head;
        ImageView msg_image;

        public OneItemHolder(@NonNull View v) {
            super(v);
            mMsg = v.findViewById(R.id.u_msg);
            head = v.findViewById(R.id.u_head);
            name = v.findViewById(R.id.u_name);
            time = v.findViewById(R.id.u_time);
            msg_image = v.findViewById(R.id.u_msg_image);
        }
    }

    /**
     * 第二个布局的Holder，要继承自RecyclerView.ViewHolder，这里你可以绑定控件
     */
    class TwoItemHolder extends RecyclerView.ViewHolder {

        TextView mMsg, name, time;
        CircleImageView head;
        ImageView msg_image;
        public TwoItemHolder(@NonNull View v) {
            super(v);

            mMsg = v.findViewById(R.id.l_msg);
            head = v.findViewById(R.id.l_head);
            name = v.findViewById(R.id.l_name);
            time = v.findViewById(R.id.l_time);
            msg_image = v.findViewById(R.id.l_msg_image);

        }
    }

    /**
     * 第三个布局的Holder，要继承自RecyclerView.ViewHolder，这里你可以绑定控件
     */
    class ThreeItemHolder extends RecyclerView.ViewHolder {

        TextView pMessage;
        TextView content,time,doctor_name,doctor_title,hosp_name,doctor_context;
        RecyclerView image_recycler;
        CircleImageView doctor_head;

        public ThreeItemHolder(@NonNull View itemView) {
            super(itemView);

            pMessage = itemView.findViewById(R.id.p_message);
            content = itemView.findViewById(R.id.content);
            time = itemView.findViewById(R.id.time);
            doctor_name = itemView.findViewById(R.id.doctor_name);
            doctor_title = itemView.findViewById(R.id.doctor_title);
            hosp_name = itemView.findViewById(R.id.hosp_name);
            doctor_context = itemView.findViewById(R.id.doctor_context);
            doctor_head = itemView.findViewById(R.id.doctor_head);
            image_recycler = itemView.findViewById(R.id.image_recycler);

        }
    }

    /**
     * 点击查看大图
     */
    public void imgMax(String url) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View imgEntryView = inflater.inflate(R.layout.to_view_image, null);
        // 加载自定义的布局文件
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        ImageView img = imgEntryView.findViewById(R.id.large_image);
        Glide.with(m_activity).load(url).error(R.drawable.icon_login_account).into(img);

        dialog.setView(imgEntryView); // 自定义dialog
        dialog.show();
        // 点击布局文件（也可以理解为点击大图）后关闭dialog，这里的dialog不需要按钮
        imgEntryView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramView) {
                dialog.cancel();
            }
        });
    }


}